package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.Comment;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.math.BigInteger;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
* 项目：his
* 创建时间：  2021-12-28 15:47:16
* 作者 :金山老师
* 描述 : 数据访问层
*/
public class CommentDao extends BaseDao<Comment> {

    @Override
    public List<Comment> selectList(String sql ) throws Exception {
        throw  new Exception("暂未实现的方法");
    }

    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<Comment> selectList(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<Comment> list = run.query(sql, new ResultSetHandler<List<Comment>>() {
            @Override
            public List<Comment> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    Comment comment = new Comment();

                    comment.setId(rs.getInt("id"));
                    comment.setAuthorId(rs.getInt("author_id"));
                    comment.setContent(rs.getString("content"));
                    comment.setCreated(rs.getTimestamp("created"));
                    comment.setPid(rs.getInt("pid"));
                    comment.setPostId(rs.getInt("post_id"));
                    comment.setStatus(rs.getInt("status"));

                    list.add(comment);
                }
                return list;
            }
        },params);

        return list;
    }




    public List<Comment> selectListWithPost(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<Comment> list = run.query(sql, new ResultSetHandler<List<Comment>>() {
            @Override
            public List<Comment> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    Comment comment = new Comment();

                    comment.setId(rs.getInt("id"));
                    comment.setAuthorId(rs.getInt("author_id"));
                    comment.setContent(rs.getString("content"));
                    comment.setCreated(rs.getTimestamp("created"));
                    comment.setPid(rs.getInt("pid"));
                    comment.setPostId(rs.getInt("post_id"));
                    comment.setStatus(rs.getInt("status"));
                    comment.setPostTitle(rs.getString("title"));

                    list.add(comment);
                }
                return list;
            }
        },params);

        return list;
    }



    /**
     *
     * @param id 查询主键
     * @return  Comment 根据主键查询的数据
     * @throws SQLException
     */
    @Override
    public Comment selectById(Integer id) throws SQLException {

        //参数列表
        Object[] params = {id};

        String sql = "select id, author_id, content, created, pid, post_id, status from mto_comment where id = ?";
        Comment comment = run.query(sql,new ResultSetHandler<Comment>() {
            @Override
            public Comment handle(ResultSet rs) throws SQLException {
                Comment comment = null;
                if(rs.next()) {
                    comment = new Comment();

                        comment.setId(rs.getInt("id"));
                        comment.setAuthorId(rs.getInt("author_id"));
                        comment.setContent(rs.getString("content"));
                        comment.setCreated(rs.getTimestamp("created"));
                        comment.setPid(rs.getInt("pid"));
                        comment.setPostId(rs.getInt("post_id"));
                        comment.setStatus(rs.getInt("status"));
                }
                return  comment;
            }
        },params);
        return comment;
    }

    /**
    *
    * @param comment 待保存的实体对象
    * @return  int 影响行数
    * @throws SQLException
    */
    @Override
    public int save(Comment comment) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO mto_comment (  ");
        sql.append("  author_id, content, created, pid, post_id, status ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("       ?,  ?,  ?,  ?,  ?,  ?  ");
        sql.append("   )  ");

        Object[] params = {
            
            comment.getAuthorId(),  
            comment.getContent(),  
            comment.getCreated(),  
            comment.getPid(),  
            comment.getPostId(),  
            comment.getStatus()
        };

        return run.update(sql.toString(),params);
    }


    /**
    * 根据主键更新
    * @param  comment
    * @return
    * @throws SQLException
    */
    @Override
    public int update(Comment comment) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   mto_comment           ");
        sql.append(" SET                    ");
            
            sql.append("   author_id  = ?,  ");
            sql.append("   content  = ?,  ");
            sql.append("   created  = ?,  ");
            sql.append("   pid  = ?,  ");
            sql.append("   post_id  = ?,  ");
            sql.append("   status  = ?");

        sql.append(" WHERE id= ?            ");

        Object[] params = {
                
                comment.getAuthorId(), 
                comment.getContent(), 
                comment.getCreated(), 
                comment.getPid(), 
                comment.getPostId(), 
                comment.getStatus(), 
            comment.getId()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from mto_comment where id = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }














}
